package com.st.config;

import com.st.entity.Goods;
import com.st.service.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.List;

//定时任务的类
@Component
@Slf4j
public class TimedTask {
    private final Logger logger=LoggerFactory.getLogger(TimedTask.class);
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private Jedis jedis;
    //定义任务的时间
    @Scheduled(fixedRate = 1000*60*60)
    public void newGoodsTask(){
        List<Goods> list=goodsService.queryNew();
        //将集合中的数据存储到redis中；
        for (int i=0; i<list.size(); i++){
            Goods g=list.get(i);
           jedis.hset("g"+i,g.toHash());
        }
        logger.info("【定时任务】--------加载新品推荐商品");
    }
}
